package com.itgacl.magic4j.modules.listener.license;

import com.itgacl.magic4j.libcommon.component.license.service.LicenseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author gacl
 * @version 1.0
 * @description: License校验监听器
 * @date 2021/12/24 10:28
 */
@Component
@Slf4j
public class LicenseValidateListener implements CommandLineRunner {

    @Value("${magic4j.license.openCheck}")
    private Boolean licenseCheck;

    @Autowired
    private LicenseService licenseService;

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);

    @Override
    public void run(String... args) {
        if(licenseCheck){
            scheduledExecutorService.scheduleWithFixedDelay(() -> licenseService.validateLicense(), 5, 30, TimeUnit.SECONDS);//延迟5秒后开始执行，以后每隔30秒执行一次
        }
    }
}
